#include <QCoreApplication>
#include "swupdateclient.h"
#include  "scriptrunner.h"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    // Set up code that uses the Qt event loop here.
    // Call a.quit() or a.exit() to quit the application.
    // A not very useful example would be including
    // #include <QTimer>
    // near the top of the file and calling
    // QTimer::singleShot(5000, &a, &QCoreApplication::quit);
    // which quits the application after 5 seconds.

    // If you do not need a running Qt event loop, remove the call
    // to a.exec() or use the Non-Qt Plain C++ Application template.
    SwUpdateClient client;
    QThread swupdateThread;

    client.moveToThread(&swupdateThread);
    swupdateThread.start();

    ScriptRunner shellRun;

    QThread shellThread;
    shellRun.moveToThread(&shellThread);

    shellThread.start();

    QObject::connect(&client, &SwUpdateClient::progressConnected, [](){
        qDebug() << " Progress Socket connect";
    });

    QObject::connect(&client, &SwUpdateClient::progressMessageReceived, [](const QByteArray &data){
        // qDebug() << "[Progress data] recv" << data.size() << "=======";
        // qDebug() << data;

        // const progress_msg *msg = reinterpret_cast<const progress_msg*>(data.constData());

        // qDebug() << "===Status:" << msg->status;
        // qDebug() << "===Current Step:" << msg->cur_step << "/" << msg->nsteps;
        // qDebug() << "===Progress:" << msg->cur_percent << "%";
        // qDebug() << "===Image:" << QString::fromUtf8(msg->cur_image);
        // qDebug() << "===Handler:" << QString::fromUtf8(msg->hnd_name);
        // qDebug() << "===Info:" << QString::fromUtf8(msg->info, msg->infolen);
    });

    QObject::connect(&client,&SwUpdateClient::progressMessageReceived,&shellRun,&ScriptRunner::recvData,Qt::QueuedConnection);

    return a.exec();
}
